﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lingopen
{
    public static class JsonHelper
    {
        public static string Serialize(this object obj)
        {
            return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
        }
        public static T Deserialize<T>(this string json)
        {
            return json == null ? default(T) : JsonConvert.DeserializeObject<T>(json);
        }
        public static List<T> DeserializeToList<T>(this string json)
        {
            return json == null ? default(List<T>) : JsonConvert.DeserializeObject<List<T>>(json);
        }

        /// <summary>
        /// 转换为json格式
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public static string ToJson(this object entity)
        {
            return JsonConvert.SerializeObject(entity, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
        }
        ///// <summary>
        ///// 转换为对象 T
        ///// </summary>
        ///// <typeparam name="T">返回值类型</typeparam>
        ///// <param name="json">json数据</param>
        ///// <returns></returns>
        //public static T FromJson<T>(this string json)
        //{
        //    return JsonConvert.DeserializeObject<T>(json);
        //}
        /// <summary>
        /// 输入净化 input = 空，则返回""
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns></returns>
        public static string Value(this string input)
        {
            return string.IsNullOrEmpty(input) ? "" : input.Trim();
        }
        /// <summary>
        /// 获取json特定值
        /// </summary>
        /// <param name="json">json数据</param>
        /// <param name="fieldName">字段名称</param>
        /// <returns></returns>
        public static string GetJsonValue(this string json, string fieldName)
        {
            try
            {
                if (string.IsNullOrEmpty(json)) return "";
                JObject jo = JObject.Parse(json);
                return jo[fieldName].ToString();
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 加载本地json文件，并进行反序列化
        /// </summary>
        /// <typeparam name="T">反序列化类型</typeparam>
        /// <param name="lockPath">文件路径</param>
        /// <returns></returns>
        public static T LoadFile<T>(this string lockPath)
        {
            if (string.IsNullOrEmpty(lockPath)) return default(T);

            string json = System.IO.File.ReadAllText(lockPath);
            if (string.IsNullOrEmpty(json)) return default(T);
            return json.FromJson<T>();
        }

        public static SiteConfig LoadSiteConfig()
        {
            var lockPath = Path.Combine(AppContext.BaseDirectory, "configs", "siteconfig.json");
            string json = System.IO.File.ReadAllText(lockPath);
            if (string.IsNullOrEmpty(json)) return new SiteConfig();
            return json.FromJson<SiteConfig>();
        }
    }
    public class SiteConfig
    {
        public EmailSetting EmailSetting { get; set; }
    }
    public class EmailSetting
    {
        /// <summary>
        /// smtp 服务器 
        /// </summary>
        public string SmtpHost { get; set; }
        /// <summary>
        /// smtp 服务器端口  默认为25
        /// </summary>
        public int SmtpPort { get; set; }
        /// <summary>
        /// 发送者 Eamil 地址
        /// </summary>
        public string FromEmailAddress { get; set; }
        /// <summary>
        /// 发送者 Eamil 密码
        /// </summary>
        public string FormEmailPassword { get; set; }
        /// <summary>
        /// 收件人 Email 列表，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string ToEmailListString { get; set; }
        /// <summary>
        /// 邮件的抄送者，支持群发，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string CcEmailListString { get; set; }
        /// <summary>
        /// 邮件的密送者，支持群发，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string BccEmail { get; set; }
    }
}
